package com.matt.foundation.utils;

import com.matt.commons.utils.DateUtil;

import java.text.SimpleDateFormat;
import java.time.Instant;
import java.time.LocalDateTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.*;

public class CookbookUtils {

    public final static String DEFINE_YYYY_MM_DD = "yyyy-MM-dd";

    public static  String dateToStr(Date date, String pattern){
        LocalDateTime time = Instant.ofEpochMilli(date.getTime()).atZone(ZoneId.systemDefault()).toLocalDateTime();
        return time.format(DateTimeFormatter.ofPattern(pattern));
    }

    public static List<Map<String, String>> getWeekList(Integer m, Integer n){
        String partten = "yyyy-MM-dd";
        Date today = new Date();
        Calendar c1 = Calendar.getInstance();
        Calendar c2 = Calendar.getInstance();
        c1.setTime(today);
        int week = c1.get(Calendar.DAY_OF_WEEK);
        week = week == 1 ? 8 : week;
        c1.add(Calendar.DATE, -week + 2 - m * 7);
        c2.add(Calendar.DATE, -week + 8 - m * 7);
        List<Map<String, String>> list = new ArrayList<Map<String, String>>();
        for (int i = -m; i <= n; i++) {
            c1.add(Calendar.DATE, 7);
            c2.add(Calendar.DATE, 7);
            Map<String, String> map = new HashMap<String, String>();
            map.put("start", dateToStr(c1.getTime(), partten));
            map.put("end", dateToStr(c2.getTime(), partten));
            if(i == -1){
                map.put("curr", "curr");
            }
            list.add(map);
        }
        return list;
    }

    public static List<String> getDayConvert(String startTime, int dayCount){
        List<String> weekConvertlist = new ArrayList<String>();
        String partten = "yyyy-MM-dd";
        Date startDate = com.matt.commons.utils.DateUtil.parseToPattern(startTime, partten);
        for(int i=0;i<dayCount;i++){
            Calendar startCal = Calendar.getInstance();
            startCal.setTime(startDate);
            int day=startCal.get(Calendar.DATE);
            startCal.set(Calendar.DATE,day+i);
            String dayAfter=new SimpleDateFormat("yyyy-MM-dd").format(startCal.getTime());
            weekConvertlist.add(dayAfter);
        }

        return weekConvertlist;
    }

    public static String getWeekOfDate(String timeString) {
        String partten = "yyyy-MM-dd";
        Date date = DateUtil.parseToPattern(timeString, partten);

        String[] weekDaysName = { "周日", "周一", "周二", "周三", "周四", "周五","周六" };
        Calendar calendar = Calendar.getInstance();
        calendar.setTime(date);
        int intWeek = calendar.get(Calendar.DAY_OF_WEEK) - 1;
        return weekDaysName[intWeek];
    }

    public static String getNowTime(String aMask) {
        Date date = new Date();
        String ret = null;
        String mask = aMask;
        if (mask == null || "".equals(mask))
            mask = "yyyy-MM-dd";
        SimpleDateFormat sdf = new SimpleDateFormat(mask);
        ret = sdf.format(date);
        return ret;
    }

    public static LinkedHashSet<String> buildDayList(Date startDate , int dayCount){
        LinkedHashSet<String> dayList = new LinkedHashSet<String>();

        for (int i = 0; i < dayCount; i++) {
            Date date = getDayBefore2Date(startDate, i);
            String dateStr = dateToStr(date, DEFINE_YYYY_MM_DD);
            dayList.add(dateStr);
        }
        return dayList;
    }

    /**
     * 构建查询日期到结束日期所有天的日期集合
     * @param
     * @param
     * @return
     */
    public static LinkedHashSet<String> buildDayList(String startTime , String endTime){

        Date startDate = DateUtil.parseToPattern(startTime, DEFINE_YYYY_MM_DD);
        Date endDate = DateUtil.parseToPattern(endTime, DEFINE_YYYY_MM_DD);
        long dayCount = DateUtil.betweenDays(startDate, endDate);

        LinkedHashSet<String> dayList = new LinkedHashSet<String>();

        for (int i = 0; i <= dayCount; i++) {
            Date date = getDayBefore2Date(startDate, i);
            String dateStr = dateToStr(date, DEFINE_YYYY_MM_DD);
            dayList.add(dateStr);
        }
        return dayList;
    }

    public static Date getDayBefore2Date(Date date, int dayNum) {
        Calendar c = Calendar.getInstance();
        if (date != null)
            c.setTime(date);

        Date now = null;
        try {
            c.add(Calendar.DATE, dayNum);
            c.set(Calendar.HOUR_OF_DAY, 0);
            c.set(Calendar.MINUTE, 0);
            c.set(Calendar.SECOND, 0);
            now = c.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }

    /**
     * 相对于当前日期的前几天(days < 0)或者后几天(days > 0) ，起始时间当前时间
     *
     * @return
     */
    public static Date getDayBefore2DateByNow(int dayNum) {
        Calendar c = Calendar.getInstance();
        Date now = null;
        try {
            c.add(Calendar.DATE, dayNum);
            now = c.getTime();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return now;
    }
}
